package com.tiantian.quanminxiu.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.adapter.RankingAdapter;
import com.tiantian.quanminxiu.adapter.WeekStartAdapter;
import com.tiantian.quanminxiu.module.Module_LinearLayoutManager;
import com.tiantian.quanminxiu.presenter.MainRankingPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.FragmentBase;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainRankingView;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.functions.Action1;

/*
* 首页
* */
public class Fragment_Main_Ranking extends FragmentBase implements MainRankingView{

    private MainRankingPresent present;

    private View linNormalRanking;
    private View rlFanDay,rlFanWeek,rlFanMonth,rlFanTotal,vFanDay,vFanWeek,vFanMonth,vFanTotal,vWeekThis,vWeekLast;
    private TextView txtFanDay,txtFanWeek,txtFanMonth,txtFanTotal;
    private RecyclerView mRecyclerView;
    private RankingAdapter adapter;
    private WeekStartAdapter weekStartAdapter;

    private int type =2;              //type =1 ;用户列表 type = 2 主播列表
    private String Sort = "Beans";        //排序方式["Beans" //艺人榜 "Coin"//富豪榜, "Anchor_Pomelo"//富豪榜,人气榜]
    private String Col = "Today";         //时间方式 ["_Today", "_Week", "_Month", "_Total"]
    private int WeekType =1 ;           //请求类型：1-艺人周星榜，2-粉丝周星榜

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_ranking, container, false);

        Init(view);
        InitView(view);

        present = new MainRankingPresent();
        present.attachView(this);

        mSwipeRefresh.setOnRefreshListener(() -> {
            UserData.Reload.Fragment_Ranking = true;
            PageInit();
        });
        UserData.Reload.Fragment_Ranking = true;
        return view;
    }
    //endregion

    public void InitView(View view){
        linNormalRanking = view.findViewById(R.id.linNormalRanking);
        rlFanDay = view.findViewById(R.id.rlFanDay);
        rlFanWeek = view.findViewById(R.id.rlFanWeek);
        rlFanMonth = view.findViewById(R.id.rlFanMonth);
        rlFanTotal = view.findViewById(R.id.rlFanTotal);
        vFanDay = view.findViewById(R.id.vFanDay);
        vFanWeek = view.findViewById(R.id.vFanWeek);
        vFanMonth = view.findViewById(R.id.vFanMonth);
        vFanTotal = view.findViewById(R.id.vFanTotal);

        txtFanDay = (TextView) view.findViewById(R.id.txtFanDay);
        txtFanWeek = (TextView) view.findViewById(R.id.txtFanWeek);
        txtFanMonth = (TextView) view.findViewById(R.id.txtFanMonth);
        txtFanTotal = (TextView) view.findViewById(R.id.txtFanTotal);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
        Module_LinearLayoutManager manager = new Module_LinearLayoutManager(thisContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        RxView.clicks(rlFanDay).map(View->rlFanDay).subscribe(OC_Button);
        RxView.clicks(rlFanWeek).map(View->rlFanWeek).subscribe(OC_Button);
        RxView.clicks(rlFanMonth).map(View->rlFanMonth).subscribe(OC_Button);
        RxView.clicks(rlFanTotal).map(View->rlFanTotal).subscribe(OC_Button);
        RxView.clicks(mHeader.txtHeader_Anchor).map(View->mHeader.txtHeader_Anchor).subscribe(OC_Button);
        RxView.clicks(mHeader.txtHeader_Regal).map(View->mHeader.txtHeader_Regal).subscribe(OC_Button);
        RxView.clicks(mHeader.txtHeader_Popularity).map(View->mHeader.txtHeader_Popularity).subscribe(OC_Button);
    }

    //region PageInit()
    private void PageInit() {
        if (!UserData.Reload.Fragment_Ranking) {
            return;
        }
        UserData.Reload.Fragment_Ranking = false;
        present.setRankingList(type,Sort,Col);
    }
    //endregion

    private Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.rlFanDay:
                vFanDay.setVisibility(View.VISIBLE);
                vFanWeek.setVisibility(View.GONE);
                vFanMonth.setVisibility(View.GONE);
                vFanTotal.setVisibility(View.GONE);
                txtFanDay.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.yellow));
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanMonth.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Col = "Today";
                present.setRankingList(type,Sort,Col);
                break;
            case R.id.rlFanWeek:
                vFanWeek.setVisibility(View.VISIBLE);
                vFanDay.setVisibility(View.GONE);
                vFanMonth.setVisibility(View.GONE);
                vFanTotal.setVisibility(View.GONE);
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.yellow));
                txtFanDay.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanMonth.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Col = "Week";
                present.setRankingList(type, Sort, Col);
                break;
            case R.id.rlFanMonth:
                vFanMonth.setVisibility(View.VISIBLE);
                vFanWeek.setVisibility(View.GONE);
                vFanDay.setVisibility(View.GONE);
                vFanTotal.setVisibility(View.GONE);
                txtFanMonth.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.yellow));
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanDay.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Col = "Month";
                present.setRankingList(type, Sort, Col);
                break;
            case R.id.rlFanTotal:
                vFanTotal.setVisibility(View.VISIBLE);
                vFanWeek.setVisibility(View.GONE);
                vFanMonth.setVisibility(View.GONE);
                vFanDay.setVisibility(View.GONE);
                txtFanTotal.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.yellow));
                txtFanWeek.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.grey3));
                txtFanMonth.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                txtFanDay.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.grey3));
                Col = "Total";
                present.setRankingList(type, Sort, Col);
                break;
            case R.id.txtHeader_Anchor:
                linNormalRanking.setVisibility(View.VISIBLE);
                mHeader.txtHeader_TitleDown.setText("艺人榜");
                mHeader.hideTpyeList();
                type = 2;
                Sort = "Beans";
                present.setRankingList(type,Sort,Col);
                break;
            case R.id.txtHeader_Regal:
                mHeader.txtHeader_TitleDown.setText("富豪榜");
                mHeader.hideTpyeList();
                type = 1;
                Sort = "Coin";
                present.setRankingList(type, Sort, Col);
                break;
            case R.id.txtHeader_Popularity:
                mHeader.txtHeader_TitleDown.setText("人气榜");
                mHeader.hideTpyeList();
                type = 2;
                Sort = "Anchor_Pomelo";
                present.setRankingList(type,Sort,Col);
                break;
        }
    };

    @Override
    public void setRankingList(JSONArray jsonArray) {
        JSONArray array = new JSONArray();
        for (int i=0;i<jsonArray.length();i++){
            JSONObject object = jsonArray.optJSONObject(i);
            if(object.optLong(Sort+"_"+Col)<=0){
                break;
            }
            array.put(object);
        }
        adapter = new RankingAdapter(thisActivity,array);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void setWeekStartList(JSONArray jsonArray) {
        weekStartAdapter = new WeekStartAdapter(thisActivity,jsonArray,WeekType);
    }

    @Override
    public void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        present.detachView();
    }
}